package com.chuiyuan.study;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chuiyuan on 2/21/16.
 */
public class PrimeGen implements Runnable {
    private final List<BigInteger> primes = new ArrayList<BigInteger>() ;
    private volatile boolean cancelled ;

    public void run() {
        BigInteger p = BigInteger.ONE;
        while (!cancelled){
            p = p.nextProbablePrime();
            synchronized (this){
                primes.add(p);//not blocking
            }
        }
    }

    public void cancel (){
        cancelled = true;
    }

    public synchronized List<BigInteger>get(){
        return new ArrayList<BigInteger>(primes);
    }
}
